// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.figures;

import org.dengues.reports.editor.ReportColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Display;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-19 qiang.zhang $
 * 
 */
public class CellFigure extends ReportElementFigure {

    private String blankString;

    /**
     * Qiang.Zhang.Adolf@gmail.com CellFigure constructor comment.
     */
    public CellFigure() {
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com CellFigure constructor comment.
     * 
     * @param image
     */
    public CellFigure(Image image) {
        super(image);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com CellFigure constructor comment.
     * 
     * @param image
     * @param alignment
     */
    public CellFigure(Image image, int alignment) {
        super(image, alignment);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.figures.ReportElementFigure#paintBorder(org.eclipse.draw2d.Graphics)
     */
    @Override
    protected void paintBorder(Graphics graphics) {
        // does nothing, table border layer paint it.
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.figures.ReportElementFigure#paintFigure(org.eclipse.draw2d.Graphics)
     */
    protected void paintFigure(Graphics graphics) {
        super.paintFigure(graphics);
        if (blankString != null && blankString.length() > 0) {
            graphics.setForegroundColor(ReportColorConstants.ShadowLineColor);
            drawBlankString(graphics, blankString);
            graphics.restoreState();
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "drawBlankString".
     * 
     * @param g
     * @param s
     */
    protected void drawBlankString(Graphics g, String s) {
        TextLayout tl = new TextLayout(Display.getCurrent());

        tl.setText(s);
        Rectangle rc = tl.getBounds();

        int left = (getClientArea().width - rc.width) / 2;
        int top = (getClientArea().height - rc.height) / 2;

        g.drawText(s, getClientArea().x + left, getClientArea().y + top);
        tl.dispose();
    }

    /**
     * @param blankString The blankString to set.
     */
    public void setBlankString(String blankString) {
        this.blankString = blankString;
    }
}
